const db = wx.cloud.database();
const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    dishi: 0,
    dishis: ['请选择报考地区', '省直', '济南', '青岛', '淄博', '枣庄', '东营', '烟台', '潍坊', '济宁', '泰安', '威海', '日照', '滨州', '德州', '聊城', '临沂', '菏泽'],
    page: 49,
    fixed: 1
  },


  kaohaoInput: function (e) {
    this.setData({
      kaohao: e.detail.value
    })
    console.log(this.data.kaohao)
  },

  onLoad: function (options) {
    var that = this;
    console.log(options);
    that.setData({
      area: options.area,
      geneal: options.agent,
      page: options.page
    })
  },

  toxn: function () {
    wx.navigateTo({
      url: '../xiaoneng/xiaoneng',
    })
  },

  tijiao: function () {
    var that = this;
    var phone = wx.getStorageSync('phone')
    var kaohao = that.data.kaohao;
    console.log(kaohao.length)
    var a = kaohao.substring(0, 1); //str
    console.log(a)
    if (kaohao.length == 13 && a == 2) {
      wx.request({ //发送请求
        url: "https://zg99.offcn.com/index/biaodan/writelogs/?actid=34868&callback=?",
        data: {
          phone: phone,
          kaohao: that.data.kaohao
        },
        success(res) {
          wx.navigateToMiniProgram({
            appId: 'wxca86930ec3e80717', //小程序appid
            path: 'page/course/details?fenxiang=1&id=268781', //跳转关联小程序app.json配置里面的地址
            //**重点**要打开的小程序版本，有效值 develop（开发版），trial（体验版），release（正式版） 
            envVersion: 'release',
            success(res) {
              console.log('打开成功！')
            }
          })
        }
      })
    } else {
      wx.showToast({
        title: '请输入正确考号！',
        icon: 'none'
      })
    }

  },



  getPhoneNumber(e) {
    var that = this;
    wx.cloud.callFunction({
      name: 'getMobile',
      data: {
        weRunData: wx.cloud.CloudID(e.detail.cloudID),
      }
    }).then(res => {
      that.setData({
        mobile: res.result,
      })
      console.log(res.result)
      wx.request({ //发送请求
        url: 'https://zg99.offcn.com/index/biaodan/register?actid=34868&callback=?', //接收url
        data: {
          phone: res.result,
          dishi: that.data.dishis[that.data.dishi],
          fenxiao: that.data.area,
          geneal: that.data.geneal,
        }, //接收参数
        success(res) {
          var jsonStr = res.data;
          jsonStr = jsonStr.replace("(", "");
          jsonStr = jsonStr.replace(")", "");
          if (typeof jsonStr != 'object') {
            jsonStr = jsonStr.replace(/\ufeff/g, ""); //字符串转化JSON对象
            var theJson = JSON.parse(jsonStr);
          }
          // console.log(theJson)
          wx.showToast({
            title: "预约成功",
            icon: 'success', //图标，支持"success"、"loading" 
            duration: 2000, //提示的延迟时间，单位毫秒，默认：1500 
            mask: false, //是否显示透明蒙层，防止触摸穿透，默认：false 
            success: function () {

              wx.request({
                url: 'https://tgadmin.sdwx.zgkfpt.cn/api/v1/wx_collect_info',
                method: 'POST',
                data: {
                  activity_id: 10,
                  mobile: that.data.mobile,
                  area_code: that.data.area,
                  extra: {
                    dishi: that.data.dishis[that.data.dishi],
                  },
                  agent: that.data.geneal,
                  page_index: that.data.page
                },
                success: res => {
                  console.log(res);
                }
              })

            },
            fail: function () {},
            complete: function () {}
          })
        }
      })



    }).catch(err => {
      console.error(err);
    });
  },



  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: '2021山东公务员补录公告预约',
      path: 'pages/index/index?by_qr=2'
    }
  },
  /**分享到朋友圈*/
  onShareTimeline: function () {
    return {
      title: '2021山东公务员补录公告预约',
      path: 'pages/index/index?by_qr=2'
    }
  }
})